package hk.m4s.lr.repair.test.ui.goods.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.jph.takephoto.model.TImage;

import java.util.ArrayList;
import java.util.List;

import framework.common.Constant;
import framework.common.widgets.dialogs.UeDialog;
import framework.common.zanetwork.core.UploadCallback;
import framework.common.zanetwork.core.UploadHelper;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.ImageModle;
import hk.m4s.lr.repair.test.ui.adapter.GridViewAdapter;
import hk.m4s.lr.repair.test.utils.CustomerAlertDialog;
import hk.m4s.lr.repair.test.utils.ToastUtil;
import hk.m4s.lr.repair.test.views.InnerGridView;

/**
 * Created by qi.wang on 2017/12/27.
 * 退款退货
 */

public class MoneyOrGoodsBackFragment extends Fragment implements AdapterView.OnItemClickListener{
    private Context context;
    private LayoutInflater inflater;
    // 图片 九宫格
    private InnerGridView gv;
    // 图片 九宫格适配器
    private GridViewAdapter gvAdapter;
    private ArrayList<TImage> imgList;
    // 用于保存图片路径
    private List<ImageModle> imgs = new ArrayList<ImageModle>();
    // 拍照
    public static final int IMAGE_CAPTURE = 1;
    // 从相册选择
    public static final int IMAGE_SELECT = 2;
    // 照片缩小比例
    private static final int SCALE = 5;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tui_goods, container, false);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        // TODO Auto-generated method stub
        context = getActivity();
        inflater = LayoutInflater.from(context);
        gv= (InnerGridView)getActivity(). findViewById(R.id.noScrollgridview);
        gvAdapter = new GridViewAdapter(getActivity(), imgs,"2");
        gv.setOnItemClickListener(this);
        gv.setAdapter(gvAdapter);
        gvAdapter.setList(imgs);

    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, final int position,
                            long id) {
        if (position == getDataSize()) {// 点击“+”号位置添加图片
            if(imgs.size()==3){
                ToastUtil.toast(context,"最大允许3张");
            }else {
                CustomerAlertDialog.showAlertDialog(getActivity(), false, "提示", new String[]{"拍照", "从图库选择", "取消"},
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                switch (which + 1) {
                                    case 1:// 拍照
//                                        CustomHelper.onClick(getTakePhoto(), 0);
                                        dialog.dismiss();
                                        break;
                                    case 2:// 从图库选择
                                        int num = 3 - imgs.size();
//                                        if (num > 0) {
//                                            CustomHelper.onClick(getTakePhoto(), 1);
//                                        }
                                        dialog.dismiss();
                                        break;
                                    case 3:// 取消
                                        dialog.dismiss();
                                        break;

                                    default:
                                        break;
                                }
                            }
                        });
            }
        } else {// 点击图片删除
            CustomerAlertDialog.showAlertDialog(getActivity(),"提示", "是否删除此图片？", "确定", "取消", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //lists.remove(position);
                    //FileUtils.delFile(list_path.get(position));
                    imgs.remove(position-1);
                    gvAdapter.setList(imgs);
                    dialog.dismiss();
                }
            }, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
        }
    }
    //    @Override
//    public void takeCancel() {
//        super.takeCancel();
//    }
//
//    @Override
//    public void takeFail(TResult result, String msg) {
//        super.takeFail(result, msg);
//    }
//
//    @Override
//    public void takeSuccess(TResult result) {
//        super.takeSuccess(result);
//        Message msg=new Message();
//        msg.obj=result.getImages();
//        handler.sendMessage(msg);
//
//
//    }
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            imgList = (ArrayList<TImage>) msg.obj;
            if(imgList!=null) {
                new UploadHelper().asyncPutObjectFromLocalFile(context, Constant.endpoint, imgList.get(0).getCompressPath(), new UploadCallback<Integer>() {
                    @Override
                    public void onRequestBefore() {
                        UeDialog.showprogressDialog(context,"","上传中...",false);
                    }

                    @Override
                    public void onFailure(PutObjectRequest response, String failureMsg, int code) {
                        UeDialog.hideProgress();
                    }

                    @Override
                    public void onSuccess(PutObjectRequest response, Integer o) {
                        UeDialog.hideProgress();
                        if (o == 200) {
                            String logo = "https://" + Constant.testBucket + ".oss-cn-beijing.aliyuncs.com" + "/" +response.getObjectKey();
                            ImageModle modle=new ImageModle();
                            modle.setImages(logo);
                            imgs.add(modle);
                            if(imgs!=null){
                                gv.setAdapter(gvAdapter);
                                gvAdapter.setList(imgs);
                            }
                        }
                    }

                    @Override
                    public void onError(PutObjectRequest response, int errorCode, Exception e) {

                    }
                });
            }
        }
    };
    private int getDataSize() {
        return imgs.size() == 0 ? 0 : imgs.size();
    }
}
